﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _4T.Data;
using _4T.DAL;

namespace _4T.BLL
{
    public class ProfileStatusBLL
    {
        public ProfileStatusBLL() { }

        public bool UpdateProfileStatus(ProfileStatus status)
        {
            ProfileStatusDAL statusDAL = new ProfileStatusDAL();
            if (status != null)
            {
                if (status.Approver != null && status.Approver.Trim().Equals(string.Empty))
                    status.Approver = null;
                if (status.Filter != null && status.Filter.Trim().Equals(string.Empty))
                    status.Filter = null;
                if (status.Verifier != null && status.Verifier.Trim().Equals(string.Empty))
                    status.Verifier = null;
                if (status.Proposer != null && status.Proposer.Trim().Equals(string.Empty))
                    status.Proposer = null;
                if (status.PaymentPerson != null && status.PaymentPerson.Trim().Equals(string.Empty))
                    status.PaymentPerson = null;
                if (status.Money != null && status.Money.Trim().Equals(string.Empty))
                    status.Money = null;
                if (status.Note != null && status.Note.Trim().Equals(string.Empty))
                    status.Note = null;
                if (status.PatientName != null && status.PatientName.Trim().Equals(string.Empty))
                    status.PatientName = null;

                CMS_ProfileInfo info = new ProfileInfoDAL().GetLastProfileInfoByProfileCode(status.ProfileCode);
                if (info != null)
                {
                    return statusDAL.UpdateProfileStatus(info.ProfileCode, status.Stage, status.Status, status.Filter, status.Filter,
                                                        status.Verifier, status.Approver, status.Money, status.Note, status.PaymentPerson);
                }

            }

            return false;
        }

        public bool UpdateProfileStatusWithProfileInfoCode(ProfileStatus status)
        {
            ProfileStatusDAL statusDAL = new ProfileStatusDAL();
            if (status != null)
            {
                return statusDAL.UpdateProfileStatus(status.ProfileCode, status.Stage, status.Status, status.Proposer, status.Filter,
                                                    status.Verifier, status.Approver, status.Money, status.Note, status.PaymentPerson, status.PaymentPerson);
            }

            return false;
        }

        public bool UpdateProfileStatusFromApp(ProfileStatus status)
        {
            bool result = false;
            // Try Catch
            try
            {
                ProfileStatusDAL statusDAL = new ProfileStatusDAL();
                if (status != null)
                {
                    if (status.Approver != null && status.Approver.Trim().Equals(string.Empty))
                        status.Approver = null;
                    if (status.Filter != null && status.Filter.Trim().Equals(string.Empty))
                        status.Filter = null;
                    if (status.Verifier != null && status.Verifier.Trim().Equals(string.Empty))
                        status.Verifier = null;
                    if (status.Proposer != null && status.Proposer.Trim().Equals(string.Empty))
                        status.Proposer = null;
                    if (status.PaymentPerson != null && status.PaymentPerson.Trim().Equals(string.Empty))
                        status.PaymentPerson = null;
                    if (status.Money != null && status.Money.Trim().Equals(string.Empty))
                        status.Money = null;
                    if (status.Note != null && status.Note.Trim().Equals(string.Empty))
                        status.Note = null;
                    if (status.PatientName != null && status.PatientName.Trim().Equals(string.Empty))
                        status.PatientName = null;

                    result = statusDAL.UpdateProfileStatus(status.ProfileCode, status.Stage, status.Status, status.Filter, status.Filter, status.Verifier, status.Approver, status.Money, status.Note, string.Empty, string.Empty);
                    if (result)
                    {
                        if (status.Status == 0)
                        {
                            new ProfileStageLinkBLL().InsertProfileStageLink(status.ProfileCode, status.Stage - 1, null, null, null, null, DateTime.Now);
                        }
                        else
                        {
                            new ProfileStageLinkBLL().InsertProfileStageLink(status.ProfileCode, status.Stage, null, null, null, null, DateTime.Now);
                        }
                        //
                        //
                        //
                        _4T.DAL.ConsiderAndDecideDAL considerAndDecideDAL = new ConsiderAndDecideDAL();
                        if (status.Stage == 4)
                        {
                            int addConsiderResult = considerAndDecideDAL.AddConsiderAndDecide(Guid.NewGuid(), string.Empty, string.Empty, string.Empty, false, false, string.Empty, string.Empty, string.Empty, false, false, string.Empty, string.Empty, string.Empty, string.Empty, status.ProfileCode, DateTime.Now);

                            if (addConsiderResult > 0)
                            {
                                result = true;
                                System.Threading.Thread thread = new System.Threading.Thread(() => threadSendNotification(status.ProfileCode));
                                thread.IsBackground = true;
                                thread.Start();
                            }
                            else
                                result = false;
                        }
                        else
                        {
                            if (status.Stage == 5)
                            {
                                Data.IDataContext.CMSConsiderAndDecide cmsConsiderAndDecide = considerAndDecideDAL.GetConsiderAndDecideByProfile(status.ProfileCode);
                                //
                                //
                                //
                                _4T.DAL.ApproveProfileDAL approveProfileDAL = new ApproveProfileDAL();
                                approveProfileDAL.AddApproveProfile(Guid.NewGuid(), string.Empty, (byte)status.Status, string.Empty, string.Empty, string.Empty, DateTime.Now, status.ProfileCode, cmsConsiderAndDecide.ConsiderAndDecideID, string.Empty);
                            }
                        }
                    }
                    //
                    //
                    //
                    return result;
                }
            }
            catch
            {
                return false;
            }
            // Try Catch
            return result;
        }

        private void threadSendNotification(string profileCode)
        {
            DAL.NotificationDAL notificationDAL = new DAL.NotificationDAL();
            notificationDAL.sendNotification(@"Hồ Sơ " + profileCode + " vừa được xác minh, vui lòng phê duyệt", 5);
        }

        public string UpdateProfileStatusWithErrorCodeReturn(ProfileStatus status)
        {
            try
            {
                ProfileStatusDAL statusDAL = new ProfileStatusDAL();
                if (status != null)
                {

                    try
                    {
                        if (status.Status == 0)
                        {
                            new ProfileStageLinkBLL().InsertProfileStageLink(status.ProfileCode, status.Stage - 1, null, null, null, null, DateTime.Now);
                        }
                        else
                        {
                            new ProfileStageLinkBLL().InsertProfileStageLink(status.ProfileCode, status.Stage, null, null, null, null, DateTime.Now);
                        }
                    }
                    catch (Exception ex)
                    {
                        return ex.ToString();
                    }
                    //
                    //
                    //
                    bool result = statusDAL.UpdateProfileStatus(status.ProfileCode, status.Stage, status.Status, status.Filter, status.Filter, status.Verifier, status.Approver, status.Money, status.Note, string.Empty, string.Empty);
                    if (result)
                    {
                        try
                        {
                            _4T.DAL.ConsiderAndDecideDAL considerAndDecideDAL = new ConsiderAndDecideDAL();
                            if (status.Stage == 4)
                            {
                                considerAndDecideDAL.AddConsiderAndDecide(Guid.NewGuid(), string.Empty, string.Empty, string.Empty, false, false, string.Empty, string.Empty, string.Empty, false, false, string.Empty, string.Empty, string.Empty, string.Empty, status.ProfileCode, DateTime.Now);
                            }
                            else
                            {
                                if (status.Stage == 5)
                                {
                                    Data.IDataContext.CMSConsiderAndDecide cmsConsiderAndDecide = considerAndDecideDAL.GetConsiderAndDecideByProfile(status.ProfileCode);
                                    //
                                    //
                                    //
                                    _4T.DAL.ApproveProfileDAL approveProfileDAL = new ApproveProfileDAL();
                                    approveProfileDAL.AddApproveProfile(Guid.NewGuid(), string.Empty, (byte)status.Status, string.Empty, string.Empty, string.Empty, DateTime.Now, status.ProfileCode, cmsConsiderAndDecide.ConsiderAndDecideID, string.Empty);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            return ex.ToString();
                        }
                    }
                    else
                        return "False";
                }
                else
                    return "Object is null";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }

            return "true";
        }

        public ProfileStatus GetProfileStatusByProfileCode(string profileCode)
        {
            return new ProfileStatusDAL().GetProfileStatusByProfileCode(profileCode);
        }

        public IEnumerable<ProfileStatus> GetAllProfileStatus()
        {
            return new ProfileStatusDAL().GetAllProfileStatus();
        }

        public ProfileStatus GetProfileStatusByProfileCodeWithPureData(string profileCode)
        {
            return new ProfileStatusDAL().GetProfileStatusByProfileCodeWithPureData(profileCode);
        }
    }
}